<?php
/**
 * PatientSignalUpload0 model class file.
 *
 * @package   models
 * @since     1.0
 * @filesource
 */

/**
 * The initial form for uploading signal. Has an organization selector.
 * 
 * PatientSignalUpload0 is a Form class. 
 * It is the data structure for keeping form data.
 */
class PatientSignalUpload0 extends CFormModel{

	/**
	 * The if for the organization
	 * @var int
	 */
	public $organizationId;
	
	/**
	 * Declares the validation rules.
	 * The rules will be used when requesting validation by call self.validate().
	 * Many class method will call self.validate(), eg: CActiveRecord.save().
	 * @return array validation rules for model attributes.
	 * @see PatientSignal
	 */
	public function rules()
	{
		return array(
			// organization id is required
			array('organizationId', 'required'),
			// organization id should be integer
			array('organizationId', 'numerical','integerOnly'=>true),
			// the user should be allowed to upload for the organization.
			array('organizationId', 'checkAccessUpload','params'=>array()),
		);
	}

	/**
	 * Returns the labels for each column variables.
	 * This will be used to render label on page.
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'organizationId' => 'Organization',
		);
	}

	/**
	 * Validation method which is declared in $this->rules();
	 * Validate if the user can upload signal file for this organization
	 * @param string $attribute
	 * @param mixed $param
	 */
	public function checkAccessUpload($attribute, $param) {
		$organization = Organization::model()->findByPk($this->organizationId);
		if(!Yii::app()->user->checkAccess('signalCreate',array(
		'organizationId'=>$this->organizationId
		))){
			$this->addError('organizationId','You do not have permission to upload for this organization.');
		}
	}
}
